/**
 * Actively Replicated Crash Free Single Software Failure (non Byzantine)
 * Distributed Retail Store 
 * 
 * November 24 2012
 */

package comp6231.project.frontend;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.omg.CORBA.ORB;
import org.omg.CosNaming.NameComponent;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.POA;

import comp6231.project.common.Constants;

/**
 * Instantiate and register retail store front ends (montreal-front-end, 
 * toronto-front-end, vancouver-front-end) with CORBA naming service.
 * 
 * @author dimitri.tiago
 */
public class FrontEndVancouver {

	/**
	 * Method main executes Front Ends application.
	 * @param args N/A
	 */
	public static void main(String[] args)
	{
		int orbPort;

		InputStreamReader is	= new InputStreamReader(System.in);
		BufferedReader br 		= new BufferedReader(is);

		try
		{
			// prompt user for CORBA registry port to use
			System.out.println("Please enter CORBA Registry port number:");
			orbPort	= 1050;//Integer.parseInt(br.readLine().trim());
			
			// obtain a new ORB instance
			String [] orbSettings = {"-ORBInitialHost", "localhost", "-ORBInitialPort", String.valueOf(orbPort)};
			ORB orb 			  = ORB.init(orbSettings, null);

			// obtain reference to root portable object adaptor and
			// activate the POAManager so that associated POAs can
			// start receiving requests
			POA rootPOA = (POA) orb.resolve_initial_references("RootPOA");
			rootPOA.the_POAManager().activate();
			
			// obtain the root naming context
			org.omg.CORBA.Object namingContext = orb.resolve_initial_references("NameService");						// obtain the naming context CORBA object
			NamingContextExt namingContextExt  = (NamingContextExt) NamingContextExtHelper.narrow(namingContext);	// obtain java naming context extension			
			
			// instantiate and register actively replicated drs front ends 
			// instantiate and register actively replicated drs front ends 
			System.out.println("Please enter IP of DRS Replica 1");	// initialize replica manager for front end
			String drsIP1 = "132.205.64.140";//br.readLine().trim();
			System.out.println("Please enter IP of DRS Replica 2");
			String drsIP2 = "132.205.64.141";//br.readLine().trim();
			System.out.println("Please enter IP of DRS Replica 3");
			String drsIP3 = "132.205.64.142";//br.readLine().trim();
			ReplicaManager rm = new ReplicaManager(Constants.RM_PORT_VANCOUVER, Constants.RM_MULTICAST_GROUP_VANCOUVER, drsIP1, drsIP2, drsIP3);
			rm.start();
			RetailStoreFrontEnd rsVancouverFE = new RetailStoreFrontEnd(rm);

			// obtain front end CORBA servant references
			org.omg.CORBA.Object rsVancouverFECORBA = rootPOA.servant_to_reference(rsVancouverFE);
			
			// obtain front end CORBA equivalent java references
			RetailStoreFrontEndInterface rsVancouverFEInterface = RetailStoreFrontEndInterfaceHelper.narrow(rsVancouverFECORBA);
						
			// bind front end object references to naming service
			String rsVancouverFEURI = String.format("!!localhost:%d!%s", orbPort, "vancouver-front-end");				
			NameComponent [] rsVancouverFEName = namingContextExt.to_name(rsVancouverFEURI);
			namingContextExt.rebind(rsVancouverFEName, rsVancouverFEInterface);

			//namingContextExt.unbind(rsVancouverName);
			
			System.out.println("Started Vancouver Retail Store Front End."); 
			
			// block thread until orb is shutdown (i.e. process incoming requests)
			orb.run();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}